#include <stdio.h>
#if 0
#include "gcd.h"
#else
/* Solves the equation ax + by = gcd(a,b). Returns gcd(a,b).*/
int egcd(int a, int b, int *px, int *py)
{
    int r, r_2 = a, r_1 = b;
    int x_2 = 1, x_1 = 0;
    int y_2 = 0, y_1 = 1;
    if (a == 0 || b == 0)
        return 0;
    while (r = r_2 % r_1)
    {
        int q = r_2 / r_1;
        int x = x_2 - q*x_1;
        int y = y_2 - q*y_1;
        x_2 = x_1; x_1 = x;
        y_2 = y_1; y_1 = y;
        r_2 = r_1; r_1 = r;
    }
    *px = x_1;
    *py = y_1;
    return r_1;
}

/* Finds a family of solutions x = x0 + k*m to the congruence equation
 *     x == a1 (mod m1) and x == a2 (mod m2). 
 * Here 0 <= x0 < m. Returns non-zero if successful, or 0 if no solution.
 */
int solve_congruence(
    int a1, int m1, int a2, int m2,
    long long *px, long long *pm)
{
    int p, q;
    int g = egcd(m1, m2, &p, &q);
    int d = a2 - a1;
    long long m, x, k;

    if (g != 1)
    {
        if (d % g != 0)
            return 0;
        m1 /= g;
        m2 /= g;
        d /= g;
    }
    k = (long long)d * p % m2;
    m = (long long)m1 * m2 * g;
    x = (k * m1 * g + a1) % m;
    if (x < 0)
        x += m;
    *px = x;
    *pm = m;
    return 1;
}

/* Find the smallest non-negative integer x that solves the congruence system
 *   x == a[0] (mod m[0])
 *   x == a[1] (mod m[1])
 *   ...
 *   x == a[N-1] (mod m[N-1])
 * If a solution cannot be found or would be greater than 'threshold', 
 * returns -1. Otherwise, returns x.
 */
int solve_congruence_system(const int m[], const int a[], int N, int threshold)
{
    int mm = m[0], aa = a[0], i;
    for (i = 1; i < N; i++)
    {
        /* Solves x == aa (mod mm) and x == a[i] (mod m[i]). */
        if (mm == -1) /* modulus exceeds threshold */
        {
            if (aa % m[i] != a[i]) /* solution would exceed threshold */
                return -1;
        }
        else
        {
            long long x, newm;
            if (!solve_congruence(aa, mm, a[i], m[i], &x, &newm))
                return -1;
            mm = (newm > threshold)? -1 : (int)newm;
            if (x > threshold)
                return -1;
            aa = (int)x;
        }
    }
    return aa;
}
#endif

int solve()
{
    const int m[3] = { 23, 28, 33 };
    int a[3], d, i, x, mm = 23*28*33;
    
    for (i = 0; i < 3; i++)
    {
        scanf("%d", &a[i]);
        a[i] %= m[i];
    }
    if (scanf("%d", &d) != 1 || d < 0)
        return 0;

    x = solve_congruence_system(m, a, 3, 100000);
    return (x > d)? x - d : mm - (d - x) % mm;
}

int main()
{
    int t, days;
    for (t = 1; (days = solve()) > 0; t++)
        printf("Case %d: the next triple peak occurs in %d days.\n", t, days);
    return 0;
}